local skel = fk.CreateSkill{
  name = "kpaII__yingkanmiaomo",
}

Fk:loadTranslationTable{
  ["kpaII__yingkanmiaomo"] = "鹰瞰淼漠",
  [":kpaII__yingkanmiaomo"] = "你使用【杀】的次数和攻击范围+X（X为横置角色数）。"..
  "回合结束时，你可固定或解除固定此时X的值，然后摸此时与回合开始时X的差值张牌。",
  ["#kpaII__yingkanmiaomo_ask1"] = "鹰瞰淼漠：你可固定X的值（%arg），然后摸 %arg2 张牌",
  ["#kpaII__yingkanmiaomo_ask2"] = "鹰瞰淼漠：你可解除固定X的值，然后摸 %arg 张牌",

  ["@[kpaII__yingkanmiaomo]"] = "鹰瞰淼漠",

  ["$kpaII__yingkanmiaomo1"] = "在我之前，神为穆斯林赢过几次战争？",
  ["$kpaII__yingkanmiaomo2"] = "（拔剑声）",
}

local KleeU = require "packages/klee_xy_I/utility/k_utility"

Fk:addQmlMark{ --标记显示对应阶段
  name = "kpaII__yingkanmiaomo",
  qml_path = "",
  how_to_show = function(name, value, p)
    return value - 1
  end,
}

---计数有多少横置
---@param player Player
---@return integer
local function getchainplayers(player)
  local n = 0
  local p = player
  while p do
    if p.chained then
      n = n + 1
    end
    p = p:getNextAlive()
    if p == player then
      break
    end
  end
  return n
end

skel:addEffect("atkrange", {--攻击范围
  correct_func = function(self, player)
    if player:hasSkill(skel.name) then
      local n
      if player:getMark("@[kpaII__yingkanmiaomo]") < 1 then
        n = getchainplayers(player)
      else
        n = player:getMark("@[kpaII__yingkanmiaomo]") - 1
      end
      return n
    end
  end,
})
skel:addEffect("targetmod", {--用杀上限
  residue_func = function(self, player, skill, scope)
    if player:hasSkill(skel.name) and skill.trueName == "slash_skill" and scope == Player.HistoryPhase then
      local n
      if player:getMark("@[kpaII__yingkanmiaomo]") < 1 then
        n = getchainplayers(player)
      else
        n = player:getMark("@[kpaII__yingkanmiaomo]") - 1
      end
      return n
    end
  end,
})

skel:addEffect(fk.TurnStart,{--记录回合开始时
  can_refresh = function (self, event, target, player, data)
    return player:hasSkill(skel.name) and target == player
  end,
  on_refresh = function (self, event, target, player, data)
    local n
    if player:getMark("@[kpaII__yingkanmiaomo]") < 1 then
      n = getchainplayers(player)
    else
      n = player:getMark("@[kpaII__yingkanmiaomo]") - 1
    end
    player.room:setPlayerMark(player,"kpaII__yingkanmiaomo_begin-turn",n)
  end
})

skel:addEffect(fk.TurnEnd,{
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(skel.name) and target == player
  end,
  on_cost = function (self, event, target, player, data)
    local prompt
    local past = player:getMark("kpaII__yingkanmiaomo_begin-turn")
    local now = getchainplayers(player)
    local add = math.abs(now - past)
    if player:getMark("@[kpaII__yingkanmiaomo]") > 0 then
      prompt = "#kpaII__yingkanmiaomo_ask2:::" .. add
    else
      prompt = "#kpaII__yingkanmiaomo_ask1:::" .. now .. ":" .. add
    end
    local yn = KleeU.askYesOrNo(player,prompt)
    if yn then
      event:setCostData(self,{now = now,add = add})
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    if player:getMark("@[kpaII__yingkanmiaomo]") > 0 then
      room:setPlayerMark(player,"@[kpaII__yingkanmiaomo]",0)
    else
      local n = event:getCostData(self).now
      room:setPlayerMark(player,"@[kpaII__yingkanmiaomo]",n+1)
    end
    local nn = event:getCostData(self).add
    if nn > 0 then
      room:drawCards(player,nn,skel.name)
    end
  end,
})

return skel